package com.mf.controller.threadFunction;

import com.mf.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: lmf
 * @Create: 2025/5/12 21:57
 * @module 线程的常用方法
 */

@RestController
@RequestMapping("threadFunction")
public class ThreadFunction {

    @Operation(summary = "获取当前线程")
    @GetMapping("getThreadName")
    public Result<String> getThreadName() {
        final Thread thread = Thread.currentThread();
        return Result.success(thread.toString() + "【线程名称+优先级+线程分组】");
    }

    @Operation(summary = "线程的让步")
    @GetMapping("yield")
    public Result<String> yield() {
        final Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("线程1: " + i);
                if (i == 20) {
                    Thread.yield();
                }
            }
        });
        final Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("线程2: " + i);
            }
        });
        t1.start();
        t2.start();
        return Result.success("【看打印信息】当线程走到i=20的时候会让出cpu资源,cpu会重新分配当然也有可能又会分配到线程1执行,但是这个概念是有的也会有这个操作");
    }

    @Operation(summary = "线程的休眠Thread.sleep")
    @GetMapping("sleep")
    public Result<String> sleep() throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        Thread.sleep(1000);
        System.out.println(System.currentTimeMillis());
        return Result.success("线程休眠完成");
    }

    @Operation(summary = "线程的强占- join")
    @GetMapping("join")
    public Result<String> join() throws InterruptedException {
        // 线程1每次循环休眠1秒
        final Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("线程t1: " + i);
            }
        });
        t1.start();
        for (int i = 0; i < 10; i++) {
            System.out.println("main" + i);
            Thread.sleep(200);
            if (i == 1) {
                // 使用join后会阻塞 2秒让t1执行两秒后它才会继续执行
                // 如果是 t1.join() 则会阻塞到t1执行完成才会继续执行
                t1.join();
            }
        }


        return Result.success("t1.join()【等待t1执行完成】 t1.join(10000)【等待t1执行10秒，如果10秒内t1执行完成了话，会直接不在阻塞】");
    }

    @Operation(summary = "线程的等待和唤醒 wait,notifyAll,notify")
    @GetMapping("wait")
    public Result<String> waitThread() throws InterruptedException {

        final Thread t1 = new Thread(() -> {
            try {
                sync();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t1");

        final Thread t2 = new Thread(() -> {
            try {
                sync();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t2");
        t1.start();
        t2.start();
        System.out.println("?/??????");
        Thread.sleep(12000);
        // 当前锁是this当
        synchronized (this) {
            System.out.println("线程被唤醒");
            // 唤醒所有线程线程 i=5进入等待后锁资源被释放在回入锁走到这里
//            ThreadFunction.class.notifyAll();
            this.notifyAll();
        }

        return Result.success();
    }

    private synchronized void sync() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                System.out.println("进入等待");
                // static 静态方法是 锁当前类
//                ThreadFunction.class.wait();
                // 私有方法是this
                this.wait();
            }
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }

    @Operation(summary = "线程的中断 interrupted")
    @GetMapping("interrupted")
    public Result<String> interrupted() throws InterruptedException {
        // 线程默认情况下，    interrupt标记位：false
        System.out.println(Thread.currentThread().isInterrupted());
        // 执行interrupt之后，再次查看打断信息
        Thread.currentThread().interrupt();
        // interrupt标记位：ture
        System.out.println(Thread.currentThread().isInterrupted());
        // 返回当前线程，并归位为false interrupt标记位：ture
        System.out.println(Thread.interrupted());
        // 已经归位了
        System.out.println(Thread.interrupted());


        Thread t1 = new Thread(() -> {
            while (true) {
                // 获取任务
                // 拿到任务，执行任务
                // 没有任务了，让线程休眠
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("基于打断形式结束当前线程");
                    return;
                }
            }

        });
        t1.start();
        Thread.sleep(500);
        System.out.println("打断线程");
        t1.interrupt();
        return Result.success("====");
    }


}
